import redis


class MyRedis():
    def __init__(self):
        self.r = redis.Redis(host='localhost', port=6379,)

    # 字段串添加
    def str_add(self, key, value):
        return self.r.set(key, value)

    # 字段串获取
    def str_get(self, key):
        value = self.r.get(key)
        if value:
            value = value.decode()
        return value

    # 字段串添加 time有效期，以秒为单位
    def str_setex(self, key, value, time):
        return self.r.setex(key, time, value)

    # 自增
    def str_incr(self, key, count):
        return self.r.incr(key, count)


# import time
r = MyRedis()
# r.str_setex('a',1,2)
# print(r.str_get('a'))
# time.sleep(2)
# # r.str_incr('a',10)
# print(r.str_get('a'))
